package com.example.danangtravel.Travel;

import com.example.danangtravel.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Transformation;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class FlowConver extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		converFlow converlfow;
		converlfow = new converFlow(this);

		ImageAdapter coverImageAdapter = new ImageAdapter(this);
		converlfow.setAdapter(coverImageAdapter);
		converlfow.setSpacing(-25);
		converlfow.setSelection(4, true);
		converlfow.setAnimationDuration(1500);
		setContentView(converlfow);
	}

	public class converFlow extends Gallery {
		private Camera mCamera = new Camera();
		private int mMaxRotationAngle = 60;
		private int mMaxZoom = -120;
		private int mCoveflowCenter;

		public converFlow(Context context) {
			super(context);
			this.setStaticTransformationsEnabled(true);
		}

		public converFlow(Context context, AttributeSet attrs) {
			super(context, attrs);
			this.setStaticTransformationsEnabled(true);
		}

		public converFlow(Context context, AttributeSet attrs, int defStyle) {
			super(context, attrs, defStyle);
			this.setStaticTransformationsEnabled(true);
		}

		public int getMaxRotationAngle() {
			return mMaxRotationAngle;
		}

		public void setMaxRotationAngle(int maxRotationAngle) {
			mMaxRotationAngle = maxRotationAngle;
		}

		public int getMaxZoom() {
			return mMaxZoom;
		}

		public void setMaxZoom(int maxZoom) {
			mMaxZoom = maxZoom;
		}

		private int getCenterOfCoverflow() {
			return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2
					+ getPaddingLeft();
		}

		private int getCenterOfView(View view) {
			return view.getLeft() + view.getWidth() / 2;
		}

		protected boolean getChildStaticTransformation(View child,
				Transformation t) {
			final int childCenter = getCenterOfView(child);
			final int childWidth = child.getWidth();
			int rotationAngle = 0;

			t.clear();
			t.setTransformationType(Transformation.TYPE_MATRIX);

			if (childCenter == mCoveflowCenter) {
				transformImageBitmap((ImageView) child, t, 0);
			} else {
				rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
				if (Math.abs(rotationAngle) > mMaxRotationAngle) {
					rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle
							: mMaxRotationAngle;
				}
				transformImageBitmap((ImageView) child, t, rotationAngle);
			}
			return true;
		}

		protected void onSizeChanged(int w, int h, int oldw, int oldh) {
			mCoveflowCenter = getCenterOfCoverflow();
			super.onSizeChanged(w, h, oldw, oldh);
		}

		private void transformImageBitmap(ImageView child, Transformation t,
				int rotationAngle) {
			mCamera.save();
			final Matrix imageMatrix = t.getMatrix();
			final int imageHeight = child.getLayoutParams().height;
			final int imageWidth = child.getLayoutParams().width;
			final int rotation = Math.abs(rotationAngle);

			mCamera.translate(0.0f, 0.0f, 100.0f);

			if (rotation < mMaxRotationAngle) {
				float zoomAmount = (float) (mMaxZoom + (rotation * 1.5));
				mCamera.translate(0.0f, 0.0f, zoomAmount);
			}

			mCamera.rotateY(rotationAngle);
			mCamera.getMatrix(imageMatrix);
			imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
			imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
			mCamera.restore();
		}
	}

	public class ImageAdapter extends BaseAdapter {
		// int mGalleryItemBackground;
		private Context mContext;
		// private List<Bitmap> listBmap;
		// private FileInputStream fis;

		private Integer[] mImageIds = { R.drawable.du1, R.drawable.du2,
				R.drawable.du3, R.drawable.du4, R.drawable.du5, R.drawable.du6,
				R.drawable.du7, R.drawable.du8, R.drawable.du9 };

		private ImageView[] mImages;

		public ImageAdapter(Context onClickListener) {
			mContext = onClickListener;
			mImages = new ImageView[mImageIds.length];
			// mImages = new ImageView[listBmap.size()];
		}

		public ImageAdapter(OnClickListener onClickListener) {
			// TODO Auto-generated constructor stub
		}

		public boolean createReflectedImages() {
			final int reflectionGap = 2;
			int index = 0;
			for (int imageId : mImageIds) {
				Bitmap originalImage = BitmapFactory.decodeResource(
						getResources(), imageId);
				int width = originalImage.getWidth();
				int height = originalImage.getHeight();

				// This will not scale but will flip on the Y axis
				Matrix matrix = new Matrix();
				matrix.preScale(1, -1);

				// Create a Bitmap with the flip matrix applied to it.
				// We only want the bottom half of the image -
				Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
						height / 2, width, height / 2, matrix, false);

				// Create a new bitmap with same width but taller to fit
				// reflection
				Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
						(height + height / 2), Config.ARGB_8888);

				// Create a new Canvas with the bitmap that's big enough for
				// the image plus gap plus reflection
				Canvas canvas = new Canvas(bitmapWithReflection);
				// Draw in the original image
				canvas.drawBitmap(originalImage, 0, 0, null);
				// Draw in the gap
				Paint deafaultPaint = new Paint();
				canvas.drawRect(0, height, width, height + reflectionGap,
						deafaultPaint);
				// Draw in the reflection
				canvas.drawBitmap(reflectionImage, 0, height + reflectionGap,
						null);

				// Create a shader that is a linear gradient that covers the
				// reflection
				Paint paint = new Paint();
				LinearGradient shader = new LinearGradient(0,
						originalImage.getHeight(), 0,
						bitmapWithReflection.getHeight() + reflectionGap,
						0x70ffffff, 0x00ffffff, TileMode.CLAMP);
				paint.setShader(shader);
				paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
				// Draw a rectangle using the paint with our linear gradient
				canvas.drawRect(0, height, width,
						bitmapWithReflection.getHeight() + reflectionGap, paint);

				ImageView imageView = new ImageView(mContext);
				imageView.setImageBitmap(bitmapWithReflection);
				imageView
						.setLayoutParams(new converFlow.LayoutParams(120, 180));
				imageView.setScaleType(ScaleType.MATRIX);
				mImages[index++] = imageView;
			}

			return true;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			// return mImageIds.length;
			return mImageIds.length;
		}

		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			ImageView i = new ImageView(mContext);
			i.setImageResource(mImageIds[position]);
			// i.setImageBitmap(listBmap.get(position));
			i.setLayoutParams(new converFlow.LayoutParams(160, 180));
			i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
			BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
			drawable.setAntiAlias(true);
			return i;
		}
	}

	// public float getScale(boolean focused, int offset) {
	// return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
	// }

}
